package com.zf.museum.service.impl;

import com.zf.museum.bean.GetResult;
import com.zf.museum.bean.Result;
import com.zf.museum.model.Administrator;
import com.zf.museum.model.Role;
import com.zf.museum.repository.AdministratorRepository;
import com.zf.museum.repository.RoleRepository;
import com.zf.museum.service.AdministratorService;
import com.zf.museum.shiro.CustomizedToken;
import com.zf.museum.shiro.LoginType;
import com.zf.museum.utils.Md5Util;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.Subject;
import java.util.List;


@Service
public class AdministatorServiceImpl implements AdministratorService{

    @Autowired
    private AdministratorRepository administratorRepository;
    @Autowired
    private RoleRepository roleRepository;
    private static final String ADMIN_LOGIN_TYPE = LoginType.ADMIN.toString();

    @Override
    public Result login(String account, String password) {

        org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
        // 指明登录类型为管理员登录(在授权时使用)
        currentUser.getSession().setAttribute("loginType", ADMIN_LOGIN_TYPE);
        CustomizedToken customizedToken = new CustomizedToken(account, password, ADMIN_LOGIN_TYPE);
        //暂时指定为false，方便测试
        System.out.println("登录未加密的密码："+password);
        password = Md5Util.getPassword(password, account);
        System.out.println("登录加密的密码："+password);
        customizedToken.setRememberMe(false);

        try {
            currentUser.login(customizedToken);
            return GetResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }
    }

    @Override
    public Result update(String password, String account) {

        Administrator admin = null;
        try {
            admin = administratorRepository.getAdministratorByAccount(account);
            /*使用md5加密*/
            password = new Md5Hash(password,admin.getAccount(), 2).toString();
            admin.setPassword(password);
            administratorRepository.save(admin);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }
        return GetResult.success();
    }

    @Override
    public Administrator getAdministrator(String account) {

        return administratorRepository.getAdministratorByAccount(account);
    }

    @Override
    public Result addAdmin(String account, String password) {

        Administrator admin = new Administrator();
        /*使用md5加密*/
        password = Md5Util.getPassword(password, account);
        System.out.println("注册加密的密码："+password);
        admin.setPassword(password);
        admin.setAccount(account);
        try {
            List<Role> roles = roleRepository.findAll();
            admin.setRoles(roles);
            administratorRepository.save(admin);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }

        return GetResult.success();
    }
}
